﻿using DemountCIM.Framework.SECSGEM;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Controls;

namespace DemountCIM.ViewModels
{
    public class MesViewModel : BindableBase
    {
        private object logLock;                                 // 日志列表锁
        private ObservableCollection<string> logMessages;       // 日志列表
        private string commState;                               // 通讯状态
        private string controlState;                            // 控制状态
        public MesViewModel()
        {
            logLock = new object();
            logMessages = new ObservableCollection<string>();
        }

        public ObservableCollection<string> LogMessages
        {
            get { return logMessages; }
            set
            {
                SetProperty(ref logMessages, value);
            }
        }

        public string CommState
        {
            get { return commState; }
            set
            {
                SetProperty(ref commState, value);
            }
        }

        public string ControlState
        {
            get { return controlState; }
            set
            {
                SetProperty(ref controlState, value);
            }
        }

        internal void SetCommState(_eCommState commState)
        {
            string tempContent = "";
            switch (commState)
            {
                case _eCommState.Comm_None:
                    tempContent = "Unknown State";
                    break;
                case _eCommState.Comm_Disabled:
                    tempContent = "Disabled";
                    break;
                case _eCommState.Comm_WaitCRFromHost:
                    tempContent = "WaitCRFromHost";
                    break;
                case _eCommState.Comm_WaitDelay:
                    tempContent = "WaitDelay";
                    break;
                case _eCommState.Comm_WaitCRA:
                    tempContent = "WaitCRA";
                    break;
                case _eCommState.Comm_Communicating:
                    tempContent = "Communicating";
                    break;
            }
            CommState = tempContent;
        }

        internal void SetControlState(_eControlState state)
        {
            string tempContent = "";
            switch (state)
            {
                case _eControlState.Control_None:
                    tempContent = "Unknown State";
                    break;
                case _eControlState.Control_EqOffline:
                    tempContent = "EqOffline";
                    break;
                case _eControlState.Control_AttemptOnline:
                    tempContent = "AttemptOnline";
                    break;
                case _eControlState.Control_HostOffline:
                    tempContent = "HostOffline";
                    break;
                case _eControlState.Control_Local:
                    tempContent = "Local";
                    break;
                case _eControlState.Control_Remote:
                    tempContent = "Remote";
                    break;
            }
            controlState = tempContent;
        }


        internal void AddMessage(string format, params object[] args)
        {
            lock (logLock)
            {
                try
                {
                    string strMsg = DateTime.Now.ToString("[HH:mm:ss fff] ") + String.Format(format, args);
                    if (Thread.CurrentThread != App.Current?.Dispatcher?.Thread)
                    {
                        if (App.Current == null)
                            return;
                        App.Current.Dispatcher.BeginInvoke(() =>
                        {
                            LogMessages.Add(strMsg);
                            if (LogMessages.Count > 100)
                            {
                                LogMessages.RemoveAt(0);
                            }
                        });
                    }
                    else
                    {
                        LogMessages.Add(strMsg);
                        if (LogMessages.Count > 100)
                        {
                            LogMessages.RemoveAt(0);
                        }
                    }
                }
                catch (Exception EX)
                {

                }
               


            }
        }

        internal void AddMessage2(string format)
        {
            lock (logLock)
            {
                if (Thread.CurrentThread != App.Current?.Dispatcher?.Thread)
                {
                    if (App.Current == null)
                        return;
                    App.Current.Dispatcher.BeginInvoke(() =>
                    {
                        LogMessages.Add(format);
                        if (LogMessages.Count > 100)
                        {
                            LogMessages.RemoveAt(0);
                        }
                    });
                }
                else
                {
                    LogMessages.Add(format);
                    if (LogMessages.Count > 100)
                    {
                        LogMessages.RemoveAt(0);
                    }
                }


            }
        }


    }
}
